package com.xoa.service.knowledge.impl;

import com.alibaba.fastjson.JSONArray;
import com.xoa.controller.IMDepart.IMDepartController;
import com.xoa.controller.users.UserPrivController;
import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.knowledge.KnowledgeColumnMapper;
import com.xoa.dao.knowledge.KnowledgeCustomerMapper;
import com.xoa.dao.knowledge.KnowledgeRoleColumnMapper;
import com.xoa.dao.users.UserPrivMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.department.Department;
import com.xoa.model.knowledge.KnowledgeColumn;
import com.xoa.model.knowledge.KnowledgeCustomer;
import com.xoa.model.knowledge.KnowledgeRoleColumn;
import com.xoa.model.users.UserPriv;
import com.xoa.model.users.Users;
import com.xoa.service.knowledge.RoleColumnService;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleColumnImpl implements RoleColumnService {

    @Autowired
    private KnowledgeCustomerMapper knowledgeCustomerMapper;

    @Autowired
    private SysCodeMapper sysCodeMapper;

    @Autowired
    private  KnowledgeColumnMapper knowledgeColumnMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private KnowledgeRoleColumnMapper knowledgeRoleColumnMapper;

    @Autowired
    private IMDepartController imDepartController;

    @Autowired
    private UserPrivController userPrivController;

    @Autowired
    private UserPrivMapper userPrivMapper;

    /**
     * 查询所有角色
     * @param roleColumn
     * @param page
     * @param limit
     * @return
     */
    @Override
    public ToJson getAllRole(HttpServletRequest request,KnowledgeRoleColumn roleColumn, Integer page, Integer limit){
        ToJson json = new ToJson(1,"err");
        Map paramMap = new HashMap<>();
        PageParams pageParams = new PageParams();
        if(page!=null&&limit!=null){ //开启分页
            pageParams.setUseFlag(true);
            pageParams.setPage(page);
            pageParams.setPageSize(limit);
            paramMap.put("page", pageParams);
        }
        if(roleColumn!=null){
            paramMap.put("roleColumn",roleColumn);
        }
        //查询所有部门
//        ToJson deptJson = imDepartController.getOrgList(request);
//        List<Department> deptList = deptJson.getObj();
//        //查询所有角色
//        ToJson<UserPriv> privJson = userPrivController.getAllPriv(new HashMap(),null,null,false,request,"1");
//        List<UserPriv> privList = privJson.getObj();
        List<KnowledgeRoleColumn> roleList = knowledgeRoleColumnMapper.selectAllByParam(paramMap);
        for(KnowledgeRoleColumn column : roleList){
//            if(column.getPrivId()!=null){
//                UserPriv up = userPrivMapper.selectByPrimaryKey(column.getPrivId());
//                column.setPrivName(up.getPrivName());
//            }
//            column.setDeptList(deptList);
//            column.setPrivList(privList);
            if(column.getColumnId()!=null){
                String[] columnIdArr = column.getColumnId().split(",");
                List<KnowledgeColumn> columnList = knowledgeColumnMapper.selectByColumnIds(columnIdArr);
                if(columnList!=null&&columnList.size()>0){
                    String coluNames = "";
                    for(KnowledgeColumn know : columnList){
                        coluNames+=know.getColumnName()+",";
                    }
                    if(coluNames.endsWith(",")){
                        coluNames = coluNames.substring(0,coluNames.length()-1);
                    }
                    column.setColumnName(coluNames);
                }
            }
        }
        if(roleList!=null&&roleList.size()>0){
            json.setData(roleList);
            json.setFlag(0);
            if(page!=null&&limit!=null){ //开启分页
                json.setTotleNum(pageParams.getTotal());
            }
            json.setMsg("查询成功");
        }else {
            json.setFlag(0);
            json.setMsg("暂无数据");
        }
        return json;
    }

    @Override
    public ToJson addRole(String jsonStr) {
        ToJson json = new ToJson(1,"err");
        if(!StringUtils.checkNull(jsonStr)){
            List<KnowledgeRoleColumn> krcList = JSONArray.parseArray(jsonStr,KnowledgeRoleColumn.class);
            if(krcList!=null&&krcList.size()>0){
                int count = 0;
                for(KnowledgeRoleColumn roleColumn : krcList){
                    String columnIds = roleColumn.getColumnId();
                    if(!StringUtils.checkNull(columnIds)){
                        String[] columnIdArr = columnIds.split(",");
                        String cIds = ",";
                        for(String cid : columnIdArr){ //简单数据验证，去掉空的，前后都加“，”
                            if(!StringUtils.checkNull(cid)){
                                cIds+=cid+",";
                            }
                        }
                        roleColumn.setColumnId(cIds);
                    }
                    roleColumn.setRoleId(null); //新增怎么可能有roleId
                    count += knowledgeRoleColumnMapper.insertSelective(roleColumn);
                }
                if(count>0){
                    json.setFlag(0);
                    json.setMsg("添加成功");
                }else {
                    json.setMsg("添加失败");
                }
            }
        }else{
            json.setMsg("参数错误");
        }
        return json;
    }

    @Override
    public ToJson delRole(String ids) {
        ToJson json = new ToJson(1,"err");
        if(!StringUtils.checkNull(ids)){
            String[] idArr = ids.split(",");
            int count = 0;
            for(String id : idArr){
                if(!StringUtils.checkNull(id)){
                    count+=knowledgeRoleColumnMapper.deleteByPrimaryKey(Integer.valueOf(id));
                }
            }
            if(count>0){
                json.setFlag(0);
                json.setMsg("删除成功");
            }else {
                json.setMsg("删除失败");
            }
        }else{
            json.setMsg("参数错误");
        }
        return json;
    }

    @Override
    public ToJson getRoleById(Integer roleId) {
        ToJson toJson = new ToJson(0,"err");
        if(roleId!=null){
            KnowledgeRoleColumn roleColumn = knowledgeRoleColumnMapper.selectByPrimaryKey(roleId);
            if(roleColumn != null){
                if(roleColumn.getColumnId()!=null){
                    String[] columnIdArr = roleColumn.getColumnId().split(",");
                    List<KnowledgeColumn> columnList = knowledgeColumnMapper.selectByColumnIds(columnIdArr);
                    if(columnList!=null&&columnList.size()>0){
                        String coluNames = "";
                        for(KnowledgeColumn know : columnList){
                            coluNames+=know.getColumnName()+",";
                        }
                        if(coluNames.endsWith(",")){
                            coluNames = coluNames.substring(0,coluNames.length()-1);
                        }
                        roleColumn.setColumnName(coluNames);
                    }
                }
                toJson.setObject(roleColumn);
                toJson.setMsg("查询成功");
                toJson.setFlag(1);
            }else{
                toJson.setMsg("没有数据");
            }
        }else {
            toJson.setMsg("参数错误");
        }
        return toJson;
    }

    @Override
    public ToJson editRole(KnowledgeRoleColumn roleColumn) {
        ToJson json = new ToJson(1,"err");
        if(roleColumn!=null){
            int count = knowledgeRoleColumnMapper.updateByPrimaryKeySelective(roleColumn);
            if(count>0){
                json.setFlag(0);
                json.setMsg("编辑成功");
            }else {
                json.setMsg("编辑失败");
            }
        }else{
            json.setMsg("参数错误");
        }
        return json;
    }
}
